package info.rflow.actions;

import info.rflow.graph.model.RBlockNode;
import info.rflow.graph.RWorkflowScene;
import info.rflow.model.RBlock;
import info.rflow.model.RWorkflowModel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;


/**
 * Document me!
 *
 * @author Holger Brandl
 */
public class ExecuteBlockAction extends AbstractAction {

    private RWorkflowScene workflowScene;


    public ExecuteBlockAction(RWorkflowScene scene) {
        this.workflowScene = scene;

        putValue(NAME, "Execute Block");
//        putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.ALT_MASK));
    }


    public void actionPerformed(ActionEvent e) {
        Set<?> selectedObjects = workflowScene.getSelectedObjects();

        Collection<RBlock> execBlocks = new ArrayList<RBlock>();

        RWorkflowModel rwm = null;
        for (Object selectedObject : selectedObjects) {
            if (selectedObject instanceof RBlockNode) {
                RBlock selBlock = ((RBlockNode) selectedObject).getRBlock();
                execBlocks.add(selBlock);
                rwm = selBlock.getWorkflowModel();
            }
        }

        if (rwm != null) {
            rwm.execute(execBlocks);
        }
    }
}
